<?php

namespace Admin\Model;

use Think\Model;
use Home\Model\UserModel;

class TougaoModel extends Model {

	/**
	 * 保存数据:当$id为空时添加数据;不为空则更新数据
	 *
	 * @param array $data
	 * @param string $id
	 */
	public function saveData($data, $id = '') {
		if (! get_cookie ( 'uid' )) {
			die ( '请登录后再操作!<a href="/">返回首页</a>' );
		}
		$id = intval ( $id );
		$this->error = false;
		if ($data ['context'] != '') {
			$data ['context'] = str_replace ( array (
					"<p><p>",
					"<p><br/></p>"
			), '', $data ['context'] );
			$data ['context'] = str_replace ( array (
					'<br>',
					'<br/>',
					'<br />'
			), '<p>', $data ['context'] );
		}
		if ($id) {
			// 编辑
			if ($data ['url'] != '') {
				if (! eregi ( "^[a-z]+[a-z0-9]+$", $data ['url'] )) {
					$this->error = 'url必须是字母开头,后面只能跟字母和数字';
				} elseif ($this->checkUrl ( $data ['url'], $id )) {
					$this->error = 'url已经存在';
				}
			}
			if ($data ['title']) {
				$_data ['title'] = $data ['title'];
			}
			if ($data ['url']) {
				$_data ['url'] = $data ['url'];
			}
			if ($data ['cid'] != '') {
				$_data ['cid'] = $data ['cid'];
			}
			if ($data ['tag'] != '') {
				$tags = make_tags ( $data ['tag'] );
				$_data ['tag'] = $tags ['str'];
			}
			if ($data ['context'] != '') {
				$_data ['context'] = strip_tags ( preg_replace ( '/\s+style\=\"[^\"]*"/i', '', $data ['context'] ), '<p>,<strong>,<img>' );
			}
			if ($data ['thumb'] != '') {
				$_data ['thumb'] = $data ['thumb'];
			}
			if ($data ['status'] != '') {
				$_data ['status'] = $data ['status'];
			}
			if ($data ['comment'] != '') {
				$_data ['comment'] = $data ['comment'];
			}
			$_data ['updatetime'] = date ( "Y-m-d H:i:s" );
			if ($this->error) {
				$result ['status'] = 0;
				$result ['info'] = $this->error;
			} else {
				$rs = $this->where ( 'id=' . $id )->save ( $_data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['info'] = '保存成功';
				} else {
					$result ['status'] = 0;
					$result ['info'] = '没有更新数据';
				}
			}
		} else {
			if (! eregi ( "^[a-z]+[a-z0-9]+$", $data ['url'] )) {
				$this->error = 'url必须是字母开头,后面只能跟字母和数字';
			} elseif (! $data ['title']) {
				$this->error = '标题不能空';
			} elseif (! $data ['url']) {
				$this->error = 'url不能空';
			} elseif (! $data ['cid']) {
				$this->error = '分类不能空';
			} elseif ($this->checkUrl ( $data ['url'], $id )) {
				$this->error = 'url已经存在';
			}
			if ($this->error) {
				$result ['status'] = 0;
				$result ['info'] = $this->error;
			} else {
				$tags = make_tags ( $data ['tag'] );
				$data ['tag'] = $tags ['str'];
				$data ['updatetime'] = $data ['createtime'] = date ( "Y-m-d H:i:s" );
				$data ['context'] = strip_tags ( preg_replace ( '/\s+style\=\"[^\"]*"/i', '', $data ['context'] ), '<p>,<strong>,<img>' );
				$data ['uid'] = get_cookie ( 'uid' );
				$rs = $this->add ( $data );
				if ($rs) {
					$result ['status'] = 1;
					$result ['info'] = '添加成功';
					$result ['id'] = $rs;
				} else {
					$result ['status'] = 0;
					$result ['info'] = '添加失败';
				}
			}
		}
		return $result;
	}

	/**
	 * 获取单条信息
	 *
	 * @param unknown $id
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, unknown, mixed, string>
	 */
	public function getInfo($id) {
		if (intval ( $id )) {
			$where = 'id=' . intval ( $id );
		} else {
			$where = 'url="' . $id . '"';
		}
		$result = $this->where ( $where )->find ();
		if ($result ['thumb']) {
			$result ['thumb500'] = C ( 'img_server' ) . str_replace ( '.', '_500_500.', $result ['thumb'] );
			$result ['thumb150'] = C ( 'img_server' ) . str_replace ( '.', '_150_150.', $result ['thumb'] );
		}
		$user = new UserModel ();
		$result ['user'] = $user->getUserInfo ( $result ['uid'] );
		return $result;
	}
	/**
	 * 搜索列表
	 *
	 * @param number $where
	 * @param number $page
	 * @param string $order
	 * @param intval $num
	 *        	每页显示条数
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = 50) {
		$page = intval ( $page ) < 1 ? 1 : $page;
		$result ['list'] = $this->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			$result ['list'] [$k] = $this->getInfo ( $v ['id'] );
		}
		$result ['total_num'] = $this->where ( $where )->count ();
		$result ['total_page'] = ceil ( $result ['total_num'] / $num );
		if ($result ['total_page'] > $page) {
			$result ['next_page'] = $page + 1;
		} else {
			$result ['next_page'] = '';
		}
		return $result;
	}

	/**
	 * 检查url是否存在
	 *
	 * @param unknown $url
	 * @param string $id
	 *        	传入自己id可以不验证
	 * @return unknown
	 */
	public function checkUrl($url, $id = '') {
		$where = 'url="' . $url . '"';
		if ($id) {
			$where .= ' and id!=' . intval ( $id );
		}
		$rs = M ( 'article' )->where ( $where )->count ();
		if (! $rs) {
			$rs = $this->where ( $where )->count ();
		}
		return $rs;
	}

	/**
	 * 删除操作(non-PHPdoc)
	 *
	 * @see \Think\Model::delete()
	 */
	public function delete($id) {
		$result ['msg'] = '删除失败';
		$result ['status'] = 0;
		if ($id) {
			// 检查是否有子信息

			$result ['msg'] = '删除成功';
			$result ['status'] = 1;
		} else {
			$result ['msg'] = '删除失败';
			$result ['status'] = 0;
		}
		return $result;
	}

	/**
	 * 设置状态
	 *
	 * @param unknown $id
	 * @param number $status
	 * @return Ambigous <boolean, unknown>
	 */
	public function setStatus($id, $status = 0) {
		$where = 'id=' . intval ( $id ) . '  ';
		$data ['status'] = intval ( $status );
		$rs = $this->where ( $where )->save ( $data );
		return $rs;
	}

	/**
	 * 设置为支付
	 *
	 * @param unknown $id
	 * @return Ambigous <boolean, unknown>
	 */
	public function setPay($id) {
		$where = 'id=' . intval ( $id ) . '  ';
		$data ['pay'] = date ( 'Y-m-d H:i:s' );
		$rs = $this->where ( $where )->save ( $data );
		return $rs;
	}
	/**
	 * 发布到线上
	 *
	 * @param unknown $id
	 * @return number Ambigous string>
	 */
	public function saveToArticle($id) {
		$this->startTrans ();
		$info = $this->getInfo ( $id );
		$article = new ArticleModel ();
		$check = $article->checkUrl ( $info ['url'] );
		// 判断是否已经存在
		if ($check) {
			$result ['msg'] = 'url已经存在';
			$result ['status'] = 0;
			return $result;
		}

		$data ['title'] = $info ['title'];
		$data ['url'] = $info ['url'];
		$data ['cid'] = $info ['cid'];
		$data ['tag'] = $info ['tag'];
		$data ['context'] = $info ['context'];
		$data ['updatetime'] = $datetime;
		$data ['createtime'] = $datetime;
		$data ['thumb'] = $info ['thumb'];
		$result = $article->saveData ( $data );
		if ($result ['status']) {
			$rs1 = $this->setStatus ( $id, 1 );
			if ($rs1) {
				$this->commit ();
			} else {
				$result ['msg'] = '状态更新失败';
				$result ['status'] = 0;
				$this->rollback ();
			}
		}

		return $result;
	}
	public function autoToArticle() {
		$time = F ( 'autoToArticle' );
		$t = 3600 * 10;
		if (time () - $time < $t) {
			$result ['msg'] = ($t / 3600) . '小时内只能发布一篇';
			$result ['status'] = 0;
			return $result;
		}
		$article = new ArticleModel ();
		$where = 'status=0';
		$rs = $this->search ( $where, 1, 'updatetime desc', 1 );
		$info = ( array ) $rs ['list'] [0];
		if (! $info ['id']) {
			$result ['msg'] = '没有可发布的文章';
			$result ['status'] = 0;
			return $result;
		}
		$check = $article->checkUrl ( $info ['url'] );
		// 判断是否已经存在
		if ($check) {
			$result ['msg'] = 'url已经存在';
			$result ['status'] = 0;
			return $result;
		}
		$id = $info ['id'];
		$datetime = date ( 'Y-m-d H:i:s' );
		$data ['title'] = $info ['title'];
		$data ['url'] = $info ['url'];
		$data ['cid'] = $info ['cid'];
		$data ['tag'] = $info ['tag'];
		$data ['context'] = $info ['context'];
		$data ['updatetime'] = $datetime;
		$data ['createtime'] = $datetime;
		$data ['thumb'] = $info ['thumb'];

		$result = $article->saveData ( $data );
		if ($result ['status']) {
			$rs1 = $this->setStatus ( $id, 1 );
			if ($rs1) {
				F ( 'autoToArticle', time () );
				$this->commit ();
			} else {
				$rs1 = $this->setStatus ( $id, - 3 );
				$result ['msg'] = '状态更新失败';
				$result ['status'] = 0;
				$this->rollback ();
			}
		}
		return $result;
	}
}